package com.ruoyi.market.service;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.bizsys.domain.MarketContactsSyncLog;
import com.ruoyi.bizsys.domain.MarketCustStatusSyncLog;
import com.ruoyi.bizsys.service.IMarketContactsSyncLogService;
import com.ruoyi.bizsys.service.IMarketCustStatusSyncLogService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.market.domain.Contact;
import com.ruoyi.market.domain.CustStatus;
import com.ruoyi.market.mapper.MarketMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: Jamnsin
 * @Date: 2021/5/8 4:29 下午
 */
@Service
public class MarketServiceImpl implements IMarketService {

    @Autowired
    MarketMapper marketMapper;

    @Autowired
    IMarketContactsSyncLogService contactsSyncLogService;

    @Autowired
    IMarketCustStatusSyncLogService custStatusSyncLogService;

    @Override
    public JSONObject getCustContacts(String level1Num, String level2Num, String level3Num) {
        String batchNo = DateUtils.dateTimeNow();

        JSONObject res = new JSONObject();
        res.put("batchNo", batchNo);

        List<Contact> contacts = new ArrayList<>();
        if (StringUtils.isNotBlank(level1Num)) {
            List<Contact> contactList = marketMapper.getCustContacts(null, Integer.parseInt(level1Num));
            contactList.forEach(p -> {
                String phoneNumber = formatPhone(p.getPhoneNumber());
                p.setPhoneNumber(phoneNumber);

            });
            res.put("level1", contactList);
            contacts.addAll(contactList);
        }

        if (StringUtils.isNotBlank(level2Num)) {
            String custLevel = "2";
            List<Contact> contactList = marketMapper.getCustContacts(custLevel, Integer.parseInt(level2Num));
            res.put("level2", contactList);
            contacts.addAll(contactList);
        }

        if (StringUtils.isNotBlank(level3Num)) {
            String custLevel = "3";
            List<Contact> contactList = marketMapper.getCustContacts(custLevel, Integer.parseInt(level3Num));
            res.put("level3", contactList);
            contacts.addAll(contactList);
        }

        contacts.stream().forEach(p -> {
            MarketContactsSyncLog contactsSyncLog = new MarketContactsSyncLog();
            contactsSyncLog.setBatchNo(batchNo);
            contactsSyncLog.setCallName(p.getCallName());
            contactsSyncLog.setPhoneNumber(p.getPhoneNumber());
            contactsSyncLog.setCountryCode(p.getCountryCode());
            contactsSyncLog.setCustLevel(p.getCustLevel());
            contactsSyncLogService.insertMarketContactsSyncLog(contactsSyncLog);
        });

        return res;
    }

    @Override
    public Map<String, Object> fetchCustStatus() {
        String batchNo = DateUtils.dateTimeNow();

        List<CustStatus> custStatusList = marketMapper.fetchCustStatus();
        custStatusList.stream().forEach(p -> {
            MarketCustStatusSyncLog custStatusSyncLog = new MarketCustStatusSyncLog();
            custStatusSyncLog.setUserId(p.getUserId());
            custStatusSyncLog.setCustStatus(p.getStatus());
            custStatusSyncLog.setBatchNo(batchNo);
            custStatusSyncLog.setCountryCode(p.getCountryCode());
            custStatusSyncLog.setPhoneNumber(p.getPhoneNumber());
            custStatusSyncLog.setIsNew(p.getIsNew());
            custStatusSyncLogService.insertMarketCustStatusSyncLog(custStatusSyncLog);
        });

        Map<String, Object> map = new HashMap<>();
        map.put("batchNo", batchNo);
        map.put("contactList", custStatusList);
        return map;
    }

    private String formatPhone(String phoneNumber) {
        phoneNumber = phoneNumber.replaceAll("-", "").replaceAll(" ", "").replaceAll("\\+", "");
        phoneNumber = phoneNumber.startsWith("0") ? "234" + phoneNumber.substring(1) : (phoneNumber.startsWith("2340") ? phoneNumber.replace("2340", "234") : phoneNumber);
        return phoneNumber;
    }

}
